package com.manager.container.service.impl;

import com.manager.common.dao.SuperDao;
import com.manager.common.utils.ObjectUtils;
import com.manager.common.vo.PageVO;
import com.manager.container.service.NetworkCardService;
import com.manager.model.DockerImages;
import com.manager.model.NetworkCard;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class NetworkCardServiceImpl extends SuperDao<Object> implements NetworkCardService {

    @Override
    public PageVO getNetworkCardPage(NetworkCard model) {
        List<Object> params = new ArrayList<>();
        String sql = " from NetworkCard t where 1 = 1 ";
        if(StringUtils.isNotEmpty(model.getName())){
            sql += " and t.name like # ";
            params.add("%"+model.getName()+"%");
        }
        sql = ObjectUtils.replaceIndexByList(sql,params);
        List<NetworkCard> list = this.getList(sql,params.toArray(),model.getStart(),model.getLimit(),"");
        int sum = this.getListCount("select count(*) " + sql,params.toArray());
        int count = (int)Math.ceil(Double.valueOf(sum)/Double.valueOf(model.getLimit()));
        return PageVO.create(model.getStart(),count,ObjectUtils.getJSONArray(new String[]{},list));
    }

    @Override
    public void addNetworkCard(NetworkCard model) {
        this.addT(model);
    }

    @Override
    public void updateNetworkCard(NetworkCard model) {
        NetworkCard networkCard = this.getT(model.getId(),NetworkCard.class);
        BeanUtils.copyProperties(model, networkCard);
        this.updateT(networkCard);
    }

    @Override
    public void deleteNetworkCard(NetworkCard model) {
        NetworkCard networkCard = this.getT(model.getId(),NetworkCard.class);
        this.delete(networkCard);
    }

    @Override
    public void updateNetworkCardStatus(NetworkCard model) {
        NetworkCard networkCard = this.getT(model.getId(),NetworkCard.class);
        networkCard.setNetworkStatus(model.getNetworkStatus());
        this.updateT(networkCard);
    }

    @Override
    public NetworkCard getNetworkCardById(NetworkCard model) {
        NetworkCard networkCard = this.getT(model.getId(),NetworkCard.class);
        return networkCard;
    }

    @Override
    public List<NetworkCard> getNetworkCardList(NetworkCard model) {
        String sql = " from  NetworkCard t  where  t.networkStatus = 1 ";
        List<NetworkCard> list = this.getList(sql,0,0,"");
        return list;
    }
}
